package top.smallway.lightbandassistant_app.UI;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import top.smallway.lightbandassistant_app.R;
import top.smallway.lightbandassistant_app.adapter.AuthorsAdapter;
import top.smallway.lightbandassistant_app.data.Authors;

/**
 * 作者列表页面
 * 用于显示歌曲的所有合作作者列表
 */
public class AuthorList extends AppCompatActivity {
    // 显示作者列表的RecyclerView
    private RecyclerView authors;
    // 存储作者数据的JSON数组
    private com.alibaba.fastjson.JSONArray collaborators;
    // 网格布局管理器
    private GridLayoutManager gridLayoutManager;
    // 作者列表适配器
    private AuthorsAdapter authorsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author_list);
        initview();

    }

    /**
     * 初始化页面视图
     * 包括初始化控件、解析作者数据并设置RecyclerView的布局和适配器
     */
    private void initview() {
        authors = findViewById(R.id.authors);
        Intent intent = getIntent();
        collaborators = com.alibaba.fastjson.JSONArray.parseArray(intent.getStringExtra("collaborators"));
        List<Authors> data = new ArrayList<>();
        for (int i = 0; i < collaborators.size(); i++) {
            Authors author = new Authors(collaborators.getJSONObject(i).getString("id"), collaborators.getJSONObject(i).getString("name"), collaborators.getJSONObject(i).getString("avatar"));
            data.add(author);
        }
        gridLayoutManager = new GridLayoutManager(this, 2);
        authors.setLayoutManager(gridLayoutManager);
        authorsAdapter = new AuthorsAdapter(data, this);
        authors.setAdapter(authorsAdapter);
    }
}